/* 
 * This file part of cget - A libcurl based downloading tool.
 * http://lianwei3.googlepages.com/home2
 * Copyright (C) 2006 Wei Lian <lianwei3@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "header.h"

extern pthread_mutex_t lock;

size_t my_write_func(void *ptr, size_t size, size_t nmemb, void *stream)
{
    pthread_mutex_lock(&lock);
    struct FileInfo *info = (struct FileInfo *) stream;
    fseek(info->fp, info->seek, SEEK_SET);
    info->seek += fwrite(ptr, size, nmemb, info->fp);
    pthread_mutex_unlock(&lock);
    return size * nmemb;
}

int my_progress_func(void *stream, double t, double d, double ut,
		     double un)
{
    struct FileInfo *info = (struct FileInfo *) stream;
    char str[128];
    sprintf(str, "(%d) download=%10.f, totoal=%10.f, (%3.f %%)",
	    info->num + 1, d, t, d * 100.0 / t);
    mvprintw(info->num + 1, 1, str);
    refresh();
    return 0;
}

void *pull_one_url(void *stream)
{
    struct FileInfo *info = (struct FileInfo *) stream;

    CURL *curl = info->curl;
    curl_easy_reset(curl);
    curl_easy_setopt(curl, CURLOPT_URL, info->url);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, stream);
    char range[64];
    sprintf(range, "%ld-%ld", info->seek, info->end);
    curl_easy_setopt(curl, CURLOPT_RANGE, range);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, stream);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
    curl_easy_perform(curl);

    return NULL;
}

size_t my_header_func(void *ptr, size_t size, size_t nmemb, void *stream)
{
    struct FileInfo *info = (struct FileInfo *) stream;
    char *str;
    if ((str = strstr((char *) ptr, "Content-Length:")))
	info->length = strtol(str + strlen("Content-Length:"), NULL, 0);
    return size * nmemb;
}

void *consistency_check(void *stream)
{
    struct FileInfo *info = (struct FileInfo *) stream;

    CURL *curl = info->curl;
    curl_easy_setopt(curl, CURLOPT_URL, info->url);
    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, my_header_func);
    curl_easy_setopt(curl, CURLOPT_WRITEHEADER, stream);
    curl_easy_setopt(curl, CURLOPT_NOBODY, TRUE);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
    curl_easy_perform(curl);

    return NULL;
}
